//
//  CGSchoolJobDetailViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/11.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGSchoolJobDetailViewController.h"
#import "ZYHTooltipView.h"

@interface CGSchoolJobDetailViewController ()<UIAlertViewDelegate>

@property(nonatomic,assign)CGFloat fontSize;
@property(nonatomic,strong)UIButton *collectionButton;
@property(nonatomic,assign)BOOL isCollection;
@property(nonatomic,strong)UIView *rightBarView;
@property(nonatomic,strong)ZYHTooltipView *toolTipView_normal;
@property(nonatomic,strong)ZYHTooltipView *toolTipView_selected;
@end

@implementation CGSchoolJobDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"校招详情";
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.fontSize = 13;
    
    
    [self initNavigationBarButton];
    [self layoutUI];
}

#pragma mark 添加导航按钮
-(void)initNavigationBarButton{
    
    //右导航按钮
    CGRect aFrame = CGRectMake(0, 0, 70, 44);
    UIView *rightBarView = [[UIView alloc]initWithFrame:aFrame];
    aFrame.size.width = 30;
    self.collectionButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.collectionButton setImage:[UIImage imageNamed:@"navigationbar_collect"] forState:UIControlStateNormal];
    [rightBarView addSubview:self.collectionButton];
    
    aFrame.origin.x += 40;
    UIButton *remindButton = [[UIButton alloc]initWithFrame:aFrame];
    [remindButton setImage:[UIImage imageNamed:@"navigationbar_notice"] forState:UIControlStateNormal];
    [remindButton setImage:[UIImage imageNamed:@"navigationbar_notice_selected"] forState:UIControlStateHighlighted];
    [rightBarView addSubview:remindButton];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarView];
    self.rightBarView = rightBarView;
    //给右导航按钮添加点击事件
    [self.collectionButton addTarget:self action:@selector(collectionButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [remindButton addTarget:self action:@selector(remindButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}

-(void)layoutUI{
    //容器View
    CGRect bFrame = CGRectMake(10, 10, SCREEN_WIDTH - 20, 120);
    UIView *containView = [[UIView alloc]initWithFrame:bFrame];
    containView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:containView];
    
    //招聘标题Label
    CGRect cFrame = CGRectMake(5, 0, SCREEN_WIDTH - 30, 44);
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:cFrame];
    titleLabel.text = self.mySchoolInfo.title;
    titleLabel.textColor = [UIColor blueColor];
    titleLabel.font = [UIFont systemFontOfSize:self.fontSize + 3];
    titleLabel.numberOfLines = 2;
    [containView addSubview:titleLabel];
    
    //公司名称Label
    cFrame.origin.y = CGRectGetMaxY(titleLabel.frame);
    cFrame.size.height = 20;
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:cFrame];
    nameLabel.text = self.mySchoolInfo.name;
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = [UIFont systemFontOfSize:self.fontSize + 1];
    [containView addSubview:nameLabel];
    
    //招聘时间Label
    cFrame.origin.y = CGRectGetMaxY(nameLabel.frame);
    cFrame.size.width = 70;
    UILabel *item1 = [[UILabel alloc]initWithFrame:cFrame];
    item1.text = @"招聘时间:";
    item1.textColor = [UIColor grayColor];
    item1.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item1];
    
    cFrame.origin.x += 70;
    cFrame.size.width = SCREEN_WIDTH - 30 - 70;
    UILabel *content1 = [[UILabel alloc]initWithFrame:cFrame];
    //content1.text = @"2015-10-26 16:07";
    content1.text = [self getTimeWithDate:self.mySchoolInfo.recruitmentTimeStart];
    content1.textColor = [UIColor grayColor];
    content1.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:content1];
    
    //工作地点Label
    cFrame = item1.frame;
    cFrame.origin.y = CGRectGetMaxY(item1.frame);;
    UILabel *item2 = [[UILabel alloc]initWithFrame:cFrame];
    item2.text = @"招聘地点:";
    item2.textColor = [UIColor grayColor];
    item2.font = [UIFont systemFontOfSize:self.fontSize];
    [containView addSubview:item2];
    
    cFrame.origin.x += 70;
    cFrame.size.width = SCREEN_WIDTH - 30 - 70;
    UILabel *content2 = [[UILabel alloc]initWithFrame:cFrame];
    content2.text = self.mySchoolInfo.recruitmentArea;
    content2.textColor = [UIColor grayColor];
    content2.font = [UIFont systemFontOfSize:self.fontSize];
    //content2.backgroundColor = [UIColor redColor];
    [containView addSubview:content2];
    
    //分隔线
    cFrame = item2.frame;
    cFrame.origin.y += 25;
    cFrame.size.height = 1;
    cFrame.size.width = titleLabel.frame.size.width;
    UIView *lineView2 = [[UIView alloc]initWithFrame:cFrame];
    lineView2.backgroundColor = [Tools getColor:@"ebebeb"];
    [containView addSubview:lineView2];
    
    //职位描述webView
    bFrame.origin.y += 110;
    bFrame.size.height = SCREEN_HEIGHT - 64 - 130;
    UIWebView *webView = [[UIWebView alloc]initWithFrame:bFrame];
    
    NSString *myContent = [self.mySchoolInfo.content stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
    myContent = [NSString stringWithFormat:@"<html><body>%@</body></html>",myContent];
    
    [webView loadHTMLString:myContent baseURL:nil];
    [self.view addSubview:webView];
    UIScrollView *scollview=(UIScrollView *)[[webView subviews]objectAtIndex:0];
    scollview.bounces=NO;
    
    //添加两个弹出框
    self.toolTipView_normal = [[ZYHTooltipView alloc]initWithSingleLineTargetView:self.rightBarView hostView:self.view tooltipText:@"收藏成功!" width:90 originOfX:SCREEN_WIDTH - 116 originOfY:0];
    self.toolTipView_normal.alpha = 0;
    
    self.toolTipView_selected = [[ZYHTooltipView alloc]initWithSingleLineTargetView:self.rightBarView hostView:self.view tooltipText:@"已经收藏!" width:90 originOfX:SCREEN_WIDTH - 116 originOfY:0];
    self.toolTipView_selected.alpha = 0;
    
}

-(NSString *)getTimeWithDate:(NSString *)timeStr{
    
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc]init];
    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
    [inputFormatter setLocale:locale];
    [inputFormatter setDateFormat:@"MMM dd, yyyy hh:mm:ss aa"];
    NSDate *inputDate = [inputFormatter dateFromString:timeStr];
    //创建日期模板对象,设置模板的显示格式
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc]init];
    outputFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
    NSString *outputDate = [outputFormatter stringFromDate:inputDate];     //自动转换成当地时间
    return outputDate;
}


#pragma mark 判断该信息是否已经被收藏
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (![CGAccountDefault shareAccount].login) {
        [self.collectionButton setImage:[UIImage imageNamed:@"navigationbar_collect"] forState:UIControlStateNormal];
        self.isCollection = NO;
        return;
    }
    
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //用户ID
    parameters[@"userId"] = [CGAccountDefault shareAccount].user_id;
    //企业招聘的ID
    parameters[@"id"] = self.mySchoolInfo.school_id;
    
    parameters[@"uid"] = [NSString stringWithFormat:@"%d",self.mySchoolInfo.uid];
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/school/showInfo.app" parameters:parameters success:^(id responseData) {
        
        NSLog(@"是否收藏请求发送成功");
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        //NSLog(@"result:%@",result);
        if ([result rangeOfString:@"true"].location != NSNotFound) {
            [self.collectionButton setImage:[UIImage imageNamed:@"navigationbar_collect_selected"] forState:UIControlStateNormal];
            self.isCollection = YES;
            
        }else{
            
            [self.collectionButton setImage:[UIImage imageNamed:@"navigationbar_collect"] forState:UIControlStateNormal];
            self.isCollection = NO;
        }
        
    } failure:^(NSError *error) {
        
        NSLog(@"是否收藏请求发送失败！");
    }];
    
}


#pragma mark 收藏学校招聘信息
-(void)collectionButtonClick{
    
    if (![CGAccountDefault shareAccount].login) {
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"亲,你还没登录呢" message:nil delegate:self cancelButtonTitle:@"看看再说" otherButtonTitles:@"马上登录", nil];
        [alertView show];
        return;
    }
    
    if (self.isCollection) {
        //[Tools alertWithTitle:@"温馨提示" andMessage:@"该信息已经被收藏!"];
        //弹出已经收藏框
        
        self.toolTipView_normal.alpha = 0;
        
        [UIView animateWithDuration:1.0 animations:^{
            
            self.toolTipView_selected.alpha = 1.0;
            
        } completion:^(BOOL finished) {

            [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                self.toolTipView_selected.alpha = 0;
                
            } completion:nil];
            
        }];

        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //用户ID
    parameters[@"userId"] = [CGAccountDefault shareAccount].user_id;
    //学校招聘的ID
    parameters[@"schoolId"] = self.mySchoolInfo.school_id;
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/collection/collectingSchool.app" parameters:parameters success:^(id responseData) {
        
        [self.collectionButton setImage:[UIImage imageNamed:@"navigationbar_collect_selected"] forState:UIControlStateNormal];
        self.isCollection = YES;
        //[Tools alertWithTitle:@"恭喜" andMessage:@"收藏成功!"];
        //弹出收藏成功弹出框
        self.toolTipView_selected.alpha = 0;
        
        [UIView animateWithDuration:1.0 animations:^{
            
            self.toolTipView_normal.alpha = 1.0;
            
        } completion:^(BOOL finished) {
            
            [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                
                self.toolTipView_normal.alpha = 0;
                
            } completion:nil];
            
        }];
        
    } failure:^(NSError *error) {
        
        [self.collectionButton setImage:[UIImage imageNamed:@"navigationbar_collect"] forState:UIControlStateNormal];
        self.isCollection = NO;
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络延时,请重试!"];
    }];
    
}


#pragma mark 跳转到添加提醒页面
-(void)remindButtonClick{
    
    CGAddRemindViewController *addRemindVC = [CGAddRemindViewController new];
    addRemindVC.remindTitle = self.mySchoolInfo.title;
    addRemindVC.school_id = self.mySchoolInfo.school_id;
    [self.navigationController pushViewController:addRemindVC animated:YES];
}

#pragma mark alertView代理方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        //跳转到登录界面
        CGLoginViewController *loginVC = [CGLoginViewController new];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}



@end
